package cn.webrx.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-05-26 12:58
 */
@Controller
public class IndexController {
    @RequestMapping({"/", "/index"})
    public String index(Model m) {
        m.addAttribute("msg", "中文信息springboot-shiro");
        return "index";
    }

    @RequestMapping("/user/add")
    public String add() {
        return "user/add";
    }


    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/check")
    public String check(String username, String password,Model model) {
        Subject su = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //token.setRememberMe(true); 记住我
        try {
            su.login(token);//登录验证操作
            System.out.println(su.isPermitted("save"));
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名不存在");
            return "login";
        }catch(IncorrectCredentialsException e){
            model.addAttribute("msg", "密码错误");
            return "login";
        } catch (Exception e) {
            //e.printStackTrace();
            return "login";
        }

        model.addAttribute("msg","账号:root，密码：123，登录成功了.");
        return "index";

    }

    @RequestMapping("/user/update")
    public String update() {
        return "user/update";
    }
}
